package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.mapper.UserMapper;
import cn.kgc.ssm.service.UserService;
import cn.kgc.ssm.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/3/1
 * @description
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseResult login(String username, String password, HttpSession session) {

        ResponseResult responseResult = new ResponseResult();
        //   password  明文  - > md5
        String md5Pwd = SecureUtil.md5(password);
        User user = userMapper.getUserByUsername(username, md5Pwd);

        if (ObjectUtils.isEmpty(user)) {
            responseResult.setCode(110);
            responseResult.setMsg("用户名或密码错误");
            return responseResult;
        }

        // 用户登录成功
        responseResult.setCode(0);
        responseResult.setMsg("认证成功");
        responseResult.setData(user);
        // 将登录成功的用户信息 缓存在会话对象中
        session.setAttribute("user",user);

        return responseResult;
    }

    @Override
    public ResponseResult selectPage(Integer page, Integer limit, String username) {

        ResponseResult responseResult = new ResponseResult();
        // 1.开启分页
        PageHelper.startPage(page, limit);
        // 2.查询数据列表
        List<User> users = userMapper.selectUsers(username);
        PageInfo<User> userPageInfo = new PageInfo<User>(users);
        // 获取用户数据总条数
        long total = userPageInfo.getTotal();
        // 3.分页数据结果的处理
        responseResult.setCode(0);
        responseResult.setMsg("查询成功");
        responseResult.setData(users);
        responseResult.setCount(total);

        return responseResult;
    }

    @Override
    public ResponseResult deleteUsers(List<Integer> collectIds) {

        // 有可能抛出异常  通过全局异常处理实现对异常的处理方案的编写
        userMapper.deleteUser(collectIds);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");

        return responseResult;
    }

    @Override
    public ResponseResult addUser(User user) {

        ResponseResult responseResult = new ResponseResult();
        // 判定用户名是否被占用
        User usr = userMapper.selectUser(user.getUsername());

        if (!ObjectUtils.isEmpty(usr)){
            responseResult.setMsg("用户名已被占用");
            responseResult.setCode(110);
            return  responseResult;
        }

        // 用户信息添加
        userMapper.insertUser(user);
        responseResult.setMsg("添加成功");
        responseResult.setCode(0);

        return responseResult;
    }

    @Override
    public ResponseResult selectUserById(Integer id) {

        ResponseResult responseResult = new ResponseResult();

        User user =  userMapper.selectById(id);

        if (ObjectUtils.isEmpty(user)){
            responseResult.setCode(110);
            responseResult.setMsg("用户已被删除");
            return  responseResult;
        }

        responseResult.setCode(0);
        responseResult.setData(user);

        return responseResult;
    }

    @Override
    public ResponseResult editUser(User user) {

        ResponseResult responseResult = new ResponseResult();
        userMapper.updateUser(user);
        responseResult.setCode(0);
        responseResult.setMsg("数据更新成功");

        return responseResult;
    }

    @Override
    public ResponseResult editPersonalInfo(User user) {

        user.setUpdateTime(new Date());
        userMapper.updateUser(user);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("个人信息更新成功");
        return responseResult;
    }

    @Override
    public ResponseResult editPersonalPassword(String oldPassword, String password, HttpSession session) {

        // 1.校验用户的旧密码是否正确 user
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        User usr= userMapper.selectUser(username);
        String mysqlPassword = usr.getPassword();  //  密文


        ResponseResult responseResult = new ResponseResult();
        if (!mysqlPassword.equals(SecureUtil.md5(oldPassword))){
            responseResult.setCode(110);
            responseResult.setMsg("原始密码不正确");
            return  responseResult;
        }

        // 2. 开始修改用户密码
        userMapper.editPwdById(usr.getId(),SecureUtil.md5(password));
        responseResult.setCode(0);
        responseResult.setMsg("密码修改成功");

        return responseResult;
    }
}
